package com.yn.club.common.util;

import java.util.Collections;
import java.util.List;
import lombok.Getter;
import lombok.Setter;
import lombok.experimental.Accessors;

/**
 * 分页响应
 *
 * @author Bing
 * @version 1.0
 **/
@Setter
@Getter
@Accessors(chain = true)
public class PageResp<T> {

  /**
   * 当前页码
   *
   * @mock 0
   */
  private long pageIndex;

  /**
   * 分页大小
   *
   * @mock 10
   */
  private long pageSize;

  /**
   * 总页数
   *
   * @mock 100
   */
  private long total;

  /**
   * 数据
   */
  private List<T> list;

  public static <T> PageResp<T> empty() {
    return of(0, 0, 0, Collections.emptyList());
  }

  public static <T> PageResp<T> single(T item) {
    return of(0, 1, 1, Collections.singletonList(item));
  }

  public static <T> PageResp<T> of(List<T> list) {
    return of(0, 0, 0, list);
  }

  public static <T> PageResp<T> of(long current, long size, long total, List<T> list) {
    return new PageResp<T>()
        .setPageIndex(current)
        .setPageSize(size)
        .setTotal(total)
        .setList(list);
  }

  public static <T> PageResp<T> of(PageResp<?> page, List<T> list) {
    return new PageResp<T>()
        .setPageIndex(page.getPageIndex())
        .setPageSize(page.getPageSize())
        .setTotal(page.getTotal())
        .setList(list);
  }

  public static <T> PageResp<T> of(
      com.baomidou.mybatisplus.extension.plugins.pagination.Page<?> page, List<T> list) {
    return new PageResp<T>()
        .setPageIndex(page.getCurrent())
        .setPageSize(page.getSize())
        .setTotal(page.getTotal())
        .setList(list);
  }

  public static <T> PageResp<T> of(org.springframework.data.domain.Page<?> page, List<T> list) {
    return new PageResp<T>()
        .setPageIndex(page.getNumber())
        .setPageSize(page.getSize())
        .setTotal(page.getTotalElements())
        .setList(list);
  }
}
